import {
    Subscribe
} from './subscribe.model'
import Dialog from '@vant/weapp/dialog/dialog'
const subscribe = new Subscribe()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        station: {},
        active: 0,
        date: [],
        subscribe: []
    },

    // 最新协议
    agreement: "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;根据最新监管要求，本平台实行实名制预约。每个手机号在所有预约平台1年内只能预约3台机动车，且手机号需经短信验证，验证通过后方可进行预约。只可预约机动车检验有效期截止前3个月内的机动车。\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;未经本人同意，擅自使用他人车辆信息预约的手机号码，将列入黑名单永不得预约。\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;预约成功后，未主动撤消预约信息，擅自不来或未按预约时段检车的“爽约”账户将被锁定6个月，到期后方可恢复预约。\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如需取消预约，最迟在预约日期前一天申请，否则按第三条进行处理。",

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        options.goods_price = parseFloat(options.goods_price)
        that.setData({
            station: options
        })
        that._loadData()
    },

    /**
     * 获取所有数据
     */
    _loadData() {
        let that = this
        that.getDate()
    },

    /**
     * 获取7天内日期
     */
    getDate() {
        let that = this
        subscribe.getDate().then((res) => {
            that.setData({
                date: res.data
            })
            let data = res.data[0],
                param = {
                    date_id: data.id,
                    company_id: that.data.station.id,
                    vehicle_id: that.data.station.vehicle_id
                }
            if ((data.week == 0 && data.status != 2) || (data.status == 1)) {
                that.setData({
                    subscribe: []
                })
            } else {
                that.getSubscribeData(param)
            }
        })
    },

    /**
     * 根据日期获取预约情况
     * @param {*} param
     */
    getSubscribeData(param) {
        let that = this
        subscribe.getSubscribeData(param).then((res) => {
            that.setData({
                subscribe: res.data
            })
        })
    },

    /**
     * 切换日期
     * @param {*} options
     */
    onSwitchStatus(options) {
        let that = this,
            id = subscribe.getDataSet(options, 'id'),
            week = subscribe.getDataSet(options, 'week'),
            status = subscribe.getDataSet(options, 'status'),
            active = subscribe.getDataSet(options, 'index'),
            param = {
                date_id: id,
                company_id: that.data.station.id,
                vehicle_id: that.data.station.vehicle_id
            }
        that.setData({
            active: active,
        })
        if ((week == 0 && status != 2) || (status == 1)) {
            that.setData({
                subscribe: []
            })
        } else {
            that.getSubscribeData(param)
        }
    },

    /**
     * 申请检车
     * @param {*} options
     */
    onApply(options) {
        let that = this,
            subscribe_id = subscribe.getDataSet(options, 'id'),
            subscribe_date = subscribe.getDataSet(options, 'subscribe_date'),
            station = that.data.station,
            param = `id=${station.id}&vehicle_name=${station.vehicle_name}&goods_id=${station.goods_id}&goods_name=${station.goods_name}&goods_price=${station.goods_price}&vehicle_id=${station.vehicle_id}&station_name=${station.station_name}&station_address=${station.station_address}&subscribe_id=${subscribe_id}&subscribe_date=${subscribe_date}&station_number=${station.station_number}`
        Dialog.confirm({
            title: '最新提示',
            message: that.agreement,
            messageAlign: 'left',
            confirmButtonText: '同意'
        }).then(() => {
            wx.navigateTo({
                url: `/pages/apply-sub/apply-sub?${param}`
            })
        }).catch(() => {
            console.log('取消')
        })
    }
})